#include <stdio.h>
#include <stdlib.h>

//结点
typedef struct _NODE
{
    int data;
    struct _NODE *next;
} NODE;

//初始化
bool InitList(NODE *&L)
{

    if (L != nullptr)
        return false;

    L = (NODE *)malloc(sizeof(NODE));
    if (L)
    {
        L->next = nullptr;
        return true;
    }
    return false;
}

//加入数据
void list_add(NODE *&L, int i, int x)
{
    NODE *per = nullptr;
    NODE *p = nullptr;
    per = L;

    while (--i && per->next)
    {

        per = per->next;
    }

    if (InitList(p))
    {

        p->data = x;
        p->next = per->next;
        per->next = p;
    }
}

//删除数据
void list_del(NODE *&L, int x)
{
    NODE *p = nullptr, *pre = L;
     p = L->next;
    while (p->data != x)
    {
        pre = p;
        p = p->next;
        if (p->next)
            return;
    }
    pre->next = p->next;
    free(p);
}

//替换数据
void list_rep(NODE *&L, int x, int k)
{
    NODE *p = nullptr;
    p = L->next;

    while (p->data != x)
    {
        p = p->next;
    }
    p->data = k;
}

//便利输出单链表
void print_list(NODE *L)
{
    NODE *p = L->next;
    int i = 0;
    printf("=================\n");
    while (p)
    {
        printf("number %d value :%d\n", ++i, p->data);
        p = p->next;
    }
}

int main()
{
    NODE *list = nullptr;

    if (!InitList(list))
        return 0;

    list_add(list, 1, 2);

    list_add(list, 1, 5);
    list_add(list, 6, 3);
    print_list(list);
    list_rep(list, 3, 6);
    print_list(list);
    list_del(list, 5);
    print_list(list);

    return 0;
}